package cn.sdjzu.exercise;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class SelectModeActivity extends BasedActivity implements
		OnClickListener {

	private Button mAddMuscleBtn;
	private Button mReducefatBtn;
	private Button mBodybuildingBtn;
	private Button mAddForceBtn;
	private Button mConstantSpeedActiveBtn;
	private Button mConstantSpeedPassiveBtn;
	private Button mConstantForceActiveBtn;
	private Button mConstantForcePassiveBtn;
	ExerciseApp mApp;
	View lineView1;
	View lineView2;
	View lineView3;
	View lineView4;
	View lineView5;
	View lineView6;
	View lineView7;
	View lineView8;
	MyView view;
	private FrameLayout test;
	Handler handler;
	String mode;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExerciseApp.getInstance().addActivity(this);
		setContentView(R.layout.select_mode);
		mApp = (ExerciseApp) SelectModeActivity.this.getApplication();

		mAddMuscleBtn = (Button) findViewById(R.id.mode_add_muscle);
		mReducefatBtn = (Button) findViewById(R.id.mode_reduce_fat);
		mBodybuildingBtn = (Button) findViewById(R.id.mode_body_building);
		mAddForceBtn = (Button) findViewById(R.id.mode_add_force);
		mConstantSpeedActiveBtn = (Button) findViewById(R.id.mode_ConstantSpeed_active);
		mConstantSpeedPassiveBtn = (Button) findViewById(R.id.mode_ConstantSpeed_passive);
		mConstantForceActiveBtn = (Button) findViewById(R.id.mode_ConstantForce_active);
		mConstantForcePassiveBtn = (Button) findViewById(R.id.mode_ConstantForce_passive);
		test = (FrameLayout) findViewById(R.id.testbitmap);
		mAddMuscleBtn.setOnClickListener(this);
		mReducefatBtn.setOnClickListener(this);
		mBodybuildingBtn.setOnClickListener(this);
		mAddForceBtn.setOnClickListener(this);
		mConstantSpeedActiveBtn.setOnClickListener(this);
		mConstantSpeedPassiveBtn.setOnClickListener(this);
		mConstantForceActiveBtn.setOnClickListener(this);
		mConstantForcePassiveBtn.setOnClickListener(this);
		// 曲线救国。。。。
		view = new MyView(SelectModeActivity.this);
		initView();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
					mode = MODE_ADD_MUSCLE;
					selectToExercise(mode, lineView1);
					break;
				case 2:
					mode = MODE_REDUCE_FAT;
					selectToExercise(mode, lineView2);
					break;
				case 3:
					mode = MODE_BODYBUILDING;
					selectToExercise(mode, lineView3);
					break;
				case 4:
					mode = MODE_ADD_FORCE;
					selectToExercise(mode, lineView4);
					break;
				case 5:
					mode = mode_ConstantSpeed_active;
					selectToExercise(mode, lineView5);
					break;
				case 6:
					mode = mode_ConstantSpeed_passive;
					selectToExercise(mode, lineView6);
					break;
				case 7:
					mode = mode_ConstantForce_active;
					selectToExercise(mode, lineView7);
					break;
				case 8:
					mode = mode_ConstantForce_passive;
					selectToExercise(mode, lineView8);
					break;

				default:
					break;
				}
			}
		};
	}

	// 页面跳转到ExerciseActivity
	public void selectToExercise(String mode, View view) {
		DrawPhoto(mode, view);
		Intent intentMM = new Intent(SelectModeActivity.this,
				ExerciseActivity.class);
		ExerciseActivity.currentExerciseMode = mode;
		startActivity(intentMM);
		SelectModeActivity.this.finish();
		overridePendingTransition(android.R.anim.fade_in,
				android.R.anim.fade_out);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.mode_add_muscle:
			Message msg1 = new Message();
			msg1.what = 1;
			handler.sendMessage(msg1);
			break;

		case R.id.mode_reduce_fat:
			Message msg2 = new Message();
			msg2.what = 2;
			handler.sendMessage(msg2);
			break;

		case R.id.mode_body_building:
			Message msg3 = new Message();
			msg3.what = 3;
			handler.sendMessage(msg3);
			break;

		case R.id.mode_add_force:
			Message msg4 = new Message();
			msg4.what = 4;
			handler.sendMessage(msg4);
			break;

		case R.id.mode_ConstantSpeed_active:
			Message msg5 = new Message();
			msg5.what = 5;
			handler.sendMessage(msg5);
			break;

		case R.id.mode_ConstantSpeed_passive:
			Message msg6 = new Message();
			msg6.what = 6;
			handler.sendMessage(msg6);
			break;

		case R.id.mode_ConstantForce_active:
			Message msg7 = new Message();
			msg7.what = 7;
			handler.sendMessage(msg7);
			break;

		case R.id.mode_ConstantForce_passive:
			Message msg8 = new Message();
			msg8.what = 8;
			handler.sendMessage(msg8);
			break;

		default:
			break;
		}
	}

	// 保存图片
	private void DrawPhoto(String mode, View view) {
		Bitmap bitmap = convertViewToBitmap(view);
		bitmap = newBitmap(bitmap);
		if (bitmap == null) {
			Toast.makeText(this, "bitmap null", Toast.LENGTH_LONG).show();
		} else {
			String filePath = SelectModeActivity.this.getCacheDir()
					.getAbsolutePath();
			Toast.makeText(SelectModeActivity.this, "filePath: " + filePath,
					Toast.LENGTH_LONG).show();
			File file = new File(filePath, "myview.png");
			if (file.exists()) {
				file.delete();
			}
			try {
				FileOutputStream out = new FileOutputStream(file);
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 获取myviewBitmap
	private Bitmap convertViewToBitmap(View view) {
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap cacheBitmap = view.getDrawingCache();
		if (cacheBitmap == null) {
			return null;
		}
		Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
				Config.ARGB_8888);
		Canvas c = new Canvas(bitmap);
		view.draw(c);
		return bitmap;
	}

	// 缩放图片
	private Bitmap newBitmap(Bitmap bitmap) {
		Bitmap newbitmap;
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		float neww = 900;
		float newh = 300;
		
		if (scale1 <= 1) {
			neww = (900 * scale1);
			newh = (300 * scale1);
		}
		// 计算缩放比例
		float scaleWidth = (neww) / w;
		float scaleHeight = (newh) / h;
		// 取得想要缩放的matrix参数
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的图片
		newbitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
		return newbitmap;
	}

	public void initView() {
		lineView1 = view.getModeView(mApp, MODE_ADD_MUSCLE);
		lineView1.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView2 = view.getModeView(mApp, MODE_REDUCE_FAT);
		 lineView2.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView3 = view.getModeView(mApp, MODE_BODYBUILDING);
		 lineView3.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView4 = view.getModeView(mApp, MODE_ADD_FORCE);
		 lineView4.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView5 = view.getModeView(mApp, MODE_BODYBUILDING);
		 lineView5.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView6 = view.getModeView(mApp, MODE_BODYBUILDING);
		 lineView6.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView7 = view.getModeView(mApp, MODE_BODYBUILDING);
		 lineView7.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView8 = view.getModeView(mApp, MODE_BODYBUILDING);
		 lineView8.setLayoutParams(new ViewGroup.LayoutParams(600, 200));
		lineView1.setVisibility(View.INVISIBLE);
		lineView2.setVisibility(View.INVISIBLE);
		lineView3.setVisibility(View.INVISIBLE);
		lineView4.setVisibility(View.INVISIBLE);
		lineView5.setVisibility(View.INVISIBLE);
		lineView6.setVisibility(View.INVISIBLE);
		lineView7.setVisibility(View.INVISIBLE);
		lineView8.setVisibility(View.INVISIBLE);
		test.addView(lineView1);
		test.addView(lineView2);
		test.addView(lineView3);
		test.addView(lineView4);
		test.addView(lineView5);
		test.addView(lineView6);
		test.addView(lineView7);
		test.addView(lineView8);
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, ControlPanelActivity.class);
		startActivity(intent);
		this.finish();
	}
}
